<?php
class Manage extends AdminBase{
	public $pageSize=15;
	
	/**
	 * 管理员列表
	 */
	public final function index(){
		$this->display('manage/list.php');
	}
	
	public final function addManagerModal(){
		$this->display('manage/manager-add-modal.php');
	}
	
	public final function addManager(){
		$para=array_merge($_POST, array(
			'parentId'=>$this->user['uid'],
			'parents'=>$this->user['parents'],
			'enable'=>1,
			'admin'=>1,
			'regTime'=>$this->time,
			'regIP'=>$this->ip(true),
			'sb'=>9
		));
		
		// 检查用户名是否重复
		$para['username']=strtolower($para['username']);
		if($this->getValue("select uid from {$this->prename}sysmember where username=?", $para['username']))
		throw new Exception('用户名已经存在');
		
		$para['password']=md5($para['password']);
		if($this->insertRow($this->prename .'sysmember', $para)){
			$id=$this->lastInsertId();
			$sql="update {$this->prename}sysmember set parents=concat(parents, ',', $id) where `uid`=$id";
			$this->update($sql);
			$this->addLog(7,$this->adminLogType[7].'['.$para['username'].']',$this->lastInsertId(),$para['username']);
			return '添加管理员成功';
		}else{
			throw new Exception('未知错误');
		}
	}
	
	public final function deleteManager($uid){
		if(!$uid=intval($uid)) throw new Exception('参数出错');
		if($uid!=$this->user['uid'])throw new Exception('别多手多脚！这个帐号不是你本人！');
		$sql="update {$this->prename}sysmember set isDelete=1 where uid=$uid";
		if($this->delete($sql)){
			$manageName=$this->getValue("select username from {$this->prename}sysmember where uid=?", $uid);
			$this->addLog(9,$this->adminLogType[9].'['.$manageName.']',$uid,$manageName);
			return '管理员已经删除';
		}else{
			throw new Exception('未知错误');
		}
	}
	
	public final function pwdManagerModal($uid){
		$this->display('manage/manager-pwd-modal.php',0,$uid);
	}
	
	public final function clearManager($uid){
		if($uid!=$this->user['uid'])throw new Exception('别多手多脚！这个帐号不是你本人！');
		$userName=$this->getValue("select username from {$this->prename}sysmember where uid=?", $uid);
		$sql="call delUser($uid)";
		if($this->update($sql)){
			$this->addLog(19,$this->adminLogType[19].'['.$userName.']',$uid,$userName);
			return '删除成功';
		}else{
			throw new Exception('删除失败');
		}
	}
	
	public final function changeManagerPwd($uid){
		if(!$uid=intval($uid)) throw new Exception('参数出错');
		if($uid!=$this->user['uid'])throw new Exception('别多手多脚！这个帐号不是你本人！');
		$manageData=$this->getRow("select password, username from {$this->prename}sysmember where uid=$uid");
		$password=$manageData['password'];
		if(!$password) throw new Exception('用户不存在');
		
		$para=$_POST;
		$para['oldpwd']=md5($para['oldpwd']);
		if($para['oldpwd']!=$password) throw new Exception('原密码不正确');
		
		unset($para['oldpwd']);
		$para['password']=md5($para['password']);
		if($para['password']==$password) throw new Exception('密码没有变化');
		if($this->updateRows($this->prename .'sysmember', $para, 'uid='.$uid)){
			$this->addLog(8,$this->adminLogType[8].'['.$manageData['username'].']',$uid,$manageData['username']);
			return '密码已经修改';
		}else{
			throw new Exception('未知出错');
		}
	}
	
	public final function loginLog(){
		include 'ip.function.php';
		$this->display('manage/login-list.php');
	}
	
	public final function km(){
		include 'ip.function.php';
		$this->display('manage/km-list.php');
	}
	
	public final function memberLevel(){
		include 'ip.function.php';
		$this->display('manage/member-level.php');
	}
	
	public final function chat() {
		include 'ip.function.php';
		$this->display('manage/chat.php');
	}
	
	public final function chaturl() {
		include 'ip.function.php';
		$this->display('manage/chaturl.php');
	}
	
	public final function chaturl_set() {
		$chaturl = $_POST['chaturl'];
		$pdo=new PDO($GLOBALS['conf']['db']['dsn'],$GLOBALS['conf']['db']['user'],$GLOBALS['conf']['db']['user']); 
		if (empty($chaturl)) {
			$data = array('code' => 0, 'result' => '不能为空');
			echo json_encode($data);exit;
		}
		$chatsql = "update chat set url = '{$chaturl}'";
		$pdo->exec($chatsql);
		
		$data = array('code' => 1, 'result' => $chaturl . '聊天室链接设置成功！');
		echo json_encode($data);exit;
	}

	public final function member_chat() {
		$p1 = $_POST['p1'];
		$p2 = $_POST['p2'];
		$p3 = $_POST['p3'];
		$p4 = $_POST['p4'];
		$p5 = $_POST['p5'];
		$p6 = $_POST['p6'];
		$pdo=new PDO($GLOBALS['conf']['db']['dsn'],$GLOBALS['conf']['db']['user'],$GLOBALS['conf']['db']['user']); 
		if (empty($p1) || empty($p2) || empty($p3) || empty($p4) || empty($p5) || empty($p6)) {
			$data = array('code' => 0, 'result' => '不能为空');
			echo json_encode($data);exit;
		}
		if ($p1 < $p2 && $p2 < $p3 && $p3 < $p4 && $p4 < $p5 && $p5 < $p6) {
			$sql = "update member_level set p1 = '{$p1}',p2 = '{$p2}',p3 = '{$p3}',p4 = '{$p4}',p5 = '{$p5}',p6 = '{$p6}'";
			$result = $pdo->exec($sql);
			if ($result) {
				$data = array('code' => 1, 'result' => '设置成功');
			}else {
				$data = array('code' => 0, 'result' => '没有做任何修改');
			}
		} else {
			$data = array('code' => 0, 'result' => '高等级的最低金额必须大于低等级的');
		}
		echo json_encode($data);exit;
	}

	public final function chat_coin() {
		$num = $_REQUEST['coin'];
		if (empty($num)) {
			$data = array('code' => 0, 'result' => '限制金额不能为空');
			echo json_encode($data);
			exit;
		}
		if (!is_numeric($num) || $num < 0) {
			$data = array('code' => 0, 'result' => '限制金额必须是整数');
			echo json_encode($data);
			exit;
		}
		$pdo=new PDO($GLOBALS['conf']['db']['dsn'],$GLOBALS['conf']['db']['user'],$GLOBALS['conf']['db']['user']);  
		$sql = "update wwk_chat_user set password = '{$num}'";
		$result = $pdo->exec($sql);
		if ($result) {
			$data = array('code' => 1, 'result' => '成功');
			echo json_encode($data);
			exit;
		}else {
			$data = array('code' => 0, 'result' => $result);
			echo json_encode($data);
			exit;
		}
	}
	
	public final function kmed(){
		$num=$_REQUEST['num'];
		$coin=$_REQUEST['coin'];
		if(empty($num)) {
			$data=array('code'=>0,'result'=>'卡密数量不能为空');
			echo json_encode($data);
			exit;
		}
		if(empty($coin)) {
			$data=array('code'=>0,'result'=>'卡密金额不能为空');
			echo json_encode($data);
			exit;
		}
		if(!is_numeric($num)||$num<0) {
			$data=array('code'=>0,'result'=>'卡密数量必须为整数');
			echo json_encode($data);
			exit;
		}
		if(!is_numeric($coin)||$coin<0) {
			$data=array('code'=>0,'result'=>'卡密金额必须为整数');
			echo json_encode($data);
			exit;
		}
		$pdo=new PDO($GLOBALS['conf']['db']['dsn'],$GLOBALS['conf']['db']['user'],$GLOBALS['conf']['db']['user']);  
		$count=0;
		$temp=array(); 
		while($connt<$num){  
			$temp[]='ZpCdSk'.mt_rand(1000000,999999).'BdPdEg'.mt_rand(1000,9999);  
			$data=array_unique($temp);  
			$connt=count($data);  
		}  
		sort($temp);
		$sqldata='';
		foreach($temp as $k=>$v) {
			$sqldata.="('{$v}','{$coin}'),";
		}
		$sqldata=trim($sqldata,',');
		$abc="('8')";
		$km1sql="insert ignore into km (km,fee) values{$sqldata}";
		$res=$pdo->exec($km1sql);
		if($res) {
			$filename='kami'.time().mt_rand(1000,9999).'.txt';
			$file=fopen($filename,'a+');
			$sql1="select * from km order by id desc limit {$res}";
			foreach($pdo->query($sql1) as $val) {
				fwrite($file,"卡密号：".$val['km']."，卡密金额：".$val['fee']."\n");
			}
			fclose($file);
			$data=array('code'=>1,'result'=>'成功,卡密文件为'.$filename,'filename'=>$filename);
			echo json_encode($data);
			exit;
		}
	}
	
	//管理日志
	public final function controlLog(){
		$this->display('manage/control-log.php');
	}
	public final function controlLogList(){
		$this->display('manage/control-log-list.php');
	}
}